package com.moon.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 密码工具类
 */
public class PasswordUtils {
    /**
     * 密码加密
     * @param password
     * @return String类型密码
     * @throws NoSuchAlgorithmException
     */
    public static String passwordEncrypt(String password) throws NoSuchAlgorithmException {
        //密码加密
        byte[] passwordBytes = password.getBytes();
        String algorithm = "SHA-256";
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        byte[] hash = messageDigest.digest(passwordBytes);
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < hash.length; i++) {
            String hex = Integer.toHexString(0xFF & hash[i]);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }

}
